package eg.ci.mvn.service.core.config.git;

import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.RuntimeUtil;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import static cn.hutool.core.text.CharSequenceUtil.isNotBlank;

/**
 * A spring controlled bean that will be injected
 * with properties about the repository state at build time.
 * This information is supplied by my plugin - <b>pl.project13.maven.git-commit-id-maven-plugin</b>
 * @author dev
 */
@JsonIgnoreProperties(value={
        "$$beanFactory",  // 避免因 GitRepositoryState$$EnhancerBySpringCGLIB 导致的序列化问题
})
@Getter
@Setter
@Configuration
@PropertySource(value = {"classpath:git.properties"}, ignoreResourceNotFound = true)
public class GitRepositoryState {
    /** ${git.tags} // comma separated tag names */
    @Value("${git.tags:}")
    String tags;

    /** ${git.branch} */
    @Value("${git.branch:}")
    String branch;

    /** ${git.dirty} */
    @Value("${git.dirty:}")
    String dirty;

    /** ${git.remote.origin.url} */
    @Value("${git.remote.origin.url:}")
    String remoteOriginUrl;

    /** ${git.commit.id.full} OR ${git.commit.id} */
    @Value("${git.commit.id:}")
    String commitId;

    /** ${git.commit.id.abbrev} */
    @Value("${git.commit.id.abbrev:}")
    String commitIdAbbrev;

    /** ${git.commit.id.describe} */
    @Value("${git.commit.id.describe:}")
    String describe;

    /** ${git.commit.id.describe-short} */
    @Value("${git.commit.id.describe-short:}")
    String describeShort;

    /** ${git.commit.user.name} */
    @Value("${git.commit.user.name:}")
    String commitUserName;

    /** ${git.commit.user.email} */
    @Value("${git.commit.user.email:}")
    String commitUserEmail;

    /** ${git.commit.message.full} */
    @Value("${git.commit.message.full:}")
    String commitMessageFull;

    /** ${git.commit.message.short} */
    @Value("${git.commit.message.short:}")
    String commitMessageShort;

    /** ${git.commit.time} */
    @Value("${git.commit.time:}")
    String commitTime;

    /** ${git.closest.tag.name} */
    @Value("${git.closest.tag.name:}")
    String closestTagName;

    /** ${git.closest.tag.commit.count} */
    @Value("${git.closest.tag.commit.count:}")
    String closestTagCommitCount;

    /** ${git.build.user.name} */
    @Value("${git.build.user.name:}")
    String buildUserName;

    /** ${git.build.user.email} */
    @Value("${git.build.user.email:}")
    String buildUserEmail;

    /** ${git.build.time} */
    @Value("${git.build.time:}")
    String buildTime;

    /** ${git.build.host} */
    @Value("${git.build.host:}")
    String buildHost;       

    /** ${git.build.version} */
    @Value("${git.build.version:}")
    String buildVersion;   

    /** ${git.build.number} */
    @Value("${git.build.number:}")
    String buildNumber;       

    /** ${git.build.number.unique} */
    @Value("${git.build.number.unique:}")
    String buildNumberUnique;


    /**主机名称 */
    String hostName;
    /**主机地址 */
    String hostAddress;

    public GitRepositoryState() {
        try {
            hostName = RuntimeUtil.execForStr("hostname").replaceAll("\r","").replaceAll("\n","");
        }catch (Exception e) {
            hostName= "";
        }
        try {
            hostAddress =  NetUtil.getLocalhostStr();
        }catch (Exception e) {
            hostAddress= "";
        }

    }

    /**
     * 获取自定义版本名
     * @return
     */
    public String getCustVersionName() {
        String versionName = "dev";
        if( isNotBlank(getBuildVersion() ) ) {
            versionName = getBuildVersion() + "." + getCommitIdAbbrev();
            if ("true".equals(getDirty())) {
                versionName += "-dev";
            }
        }
        return versionName;
    }
}
